﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ApprovalService.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the ApprovalService type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Services
{
  using System;
  using BusinessEngineInterfaces;
  using Entities;
  using Exceptions;
  using ServiceInterfaces;

  public class ApprovalService : IApprovalService
  {
    private readonly IApprovalEngine approvalEngine;

    public ApprovalService(IApprovalEngine approvalEngine)
    {
      this.approvalEngine = approvalEngine;
    }

    public void ApproveDocument(String userName, String documentId, String manCo, String docType, String subDocType)
    {
      try
      {
        this.approvalEngine.ApproveDocument(userName, documentId);
      }
      catch (Exception e)
      {
        if (e is DocumentAlreadyApprovedException || e is DocumentAlreadyRejectedException)
        {
          throw;
        }

        throw new UnityException(string.Format("Unable to approve document guid {0}", documentId), e);
      }
    }

    public BatchApprovalResults ApproveGrid(String username, String grid)
    {
      try
      {
        return this.approvalEngine.ApproveGrid(grid, username);
      }
      catch (Exception e)
      {
        throw new UnityException(string.Format("Unable to approve grid {0}", grid), e);
      }
    }

    public BatchApprovalResults ApproveBasket(String username, String cartId)
    {
      try
      {
        return this.approvalEngine.ApproveBasket(username, cartId);
      }
      catch (Exception e)
      {
        throw new UnityException(string.Format("Unable to approve basket for {0}", cartId), e);
      }
    }
  }
}
